package com.lb.shopguide.adapter;

import android.graphics.Color;
import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.StyleSpan;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.TimeUtils;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.lb.shopguide.R;
import com.lb.shopguide.constants.AppConstant;
import com.lb.shopguide.entity.BossImMessageBean;
import com.lb.shopguide.entity.BossMessageBean;
import com.lb.shopguide.entity.GuidanceBean;
import com.lb.shopguide.entity.GuideStepBean;
import com.lb.shopguide.util.lb.BizUtil;
import com.lb.shopguide.util.pic.ImageLoaderUtils;
import com.yqritc.recyclerviewflexibledivider.FlexibleDividerDecoration;

import java.util.List;

/**
 * Created by kang on 2018/3/2.
 */

public class AdapterBossMessage extends BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder> implements FlexibleDividerDecoration.PaintProvider {
    private boolean isHome;

    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public AdapterBossMessage(List<MultiItemEntity> data, boolean isHome) {
        super(data);
        this.isHome = isHome;
        addItemType(AppConstant.MESSAGE_IM_MESSAGE, R.layout.item_im_message);
        addItemType(AppConstant.MESSAGE_GUIDE_MESSAGE, R.layout.layout_guider_view);
        addItemType(AppConstant.MESSAGE_MERCHANT_ACTIVITY, R.layout.item_merchant_activity);
        addItemType(AppConstant.MESSAGE_INCOME_MEMBER_DAILY, R.layout.item_income_member_daily);
        addItemType(AppConstant.MESSAGE_MEMBER_MONTHLY, R.layout.item_balance_not_enough);
        addItemType(AppConstant.MESSAGE_INCOME_MONTHLY, R.layout.item_balance_not_enough);
        addItemType(AppConstant.MESSAGE_BALANCE_NOT_ENOUGH, R.layout.item_balance_not_enough);
        addItemType(AppConstant.MESSAGE_MARKET_ADVICE, R.layout.item_market_advice);
        addItemType(AppConstant.MESSAGE_ACTIVITY_APPLY, R.layout.item_secretary_activity_apply);
    }

    @Override
    protected void convert(BaseViewHolder helper, final MultiItemEntity item) {
        View view = helper.itemView;
        TextView tvMessageContent;
        TextView tvMessageTime;
        TextView tvMessageTitle;
        ImageView ivIgnore;
        final BossMessageBean messageBean;
        switch (helper.getItemViewType()) {
            case AppConstant.MESSAGE_MERCHANT_ACTIVITY:
                messageBean = (BossMessageBean) item;

                tvMessageTime = view.findViewById(R.id.tv_message_time);
                tvMessageTime.setText(TimeUtils.getFriendlyTimeSpanByNow(messageBean.getCreateTime()));

                ImageView ivActivityBack = view.findViewById(R.id.iv_activity_back);
                Button btnJoin = view.findViewById(R.id.btn_join_activity);

                ImageLoaderUtils.displayRound(mContext, ivActivityBack, messageBean.getMsgIconUrl(), 4);
                btnJoin.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (mOnMsgManagerListener != null) {
                            mOnMsgManagerListener.gotoActivity(messageBean.getMsgLinkUrl(), "");
                        }
                    }
                });
                break;
            case AppConstant.MESSAGE_INCOME_MEMBER_DAILY:
                messageBean = (BossMessageBean) item;
                tvMessageTime = view.findViewById(R.id.tv_message_time);
                tvMessageTitle = view.findViewById(R.id.tv_message_title);
                ivIgnore = view.findViewById(R.id.iv_ignore);
                if (isHome) {
                    ivIgnore.setVisibility(View.VISIBLE);
                    ivIgnore.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if (mOnMsgManagerListener != null) {
                                mOnMsgManagerListener.onMsgClose(messageBean.getAppMsgCode());
                            }
                        }
                    });
                } else {
                    ivIgnore.setVisibility(View.GONE);
                }
                TextView tvMessageIncome = view.findViewById(R.id.tv_message_content_income);
                TextView tvMessageMember = view.findViewById(R.id.tv_message_content_member);

                tvMessageTime.setText(TimeUtils.getFriendlyTimeSpanByNow(messageBean.getCreateTime()));
                tvMessageTitle.setText(messageBean.getMsgTitle());

                String content = messageBean.getMsgContent();
                String msgIncome = content.split("&")[0];
                String msgMember = content.split("&")[1];
//                tvMessageIncome.setText(msgIncome);
//                tvMessageMember.setText(msgMember);
                setBoldNumber(msgIncome, tvMessageIncome);
                setBoldNumber(msgMember, tvMessageMember);
                break;
            case AppConstant.MESSAGE_MEMBER_MONTHLY:
            case AppConstant.MESSAGE_INCOME_MONTHLY:
            case AppConstant.MESSAGE_BALANCE_NOT_ENOUGH:
                messageBean = (BossMessageBean) item;
                tvMessageContent = view.findViewById(R.id.tv_message_content);
                tvMessageTime = view.findViewById(R.id.tv_message_time);
                tvMessageTitle = view.findViewById(R.id.tv_message_title);
                ivIgnore = view.findViewById(R.id.iv_ignore);
                if (isHome) {
                    ivIgnore.setVisibility(View.VISIBLE);
                    ivIgnore.setVisibility(View.VISIBLE);
                    ivIgnore.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if (mOnMsgManagerListener != null) {
                                mOnMsgManagerListener.onMsgClose(messageBean.getAppMsgCode());
                            }
                        }
                    });
                } else {
                    ivIgnore.setVisibility(View.GONE);
                }

//                LogUtils.d("fTime========",TimeUtils.getFriendlyTimeSpanByNow(messageBean.getCreateTime()));
//                LogUtils.d("Time========",messageBean.getCreateTime());
//                LogUtils.d("Now========", TimeUtils.getNowTimeString());
                tvMessageTime.setText(TimeUtils.getFriendlyTimeSpanByNow(messageBean.getCreateTime()));
                tvMessageTitle.setText(messageBean.getMsgTitle());
                if (helper.getItemViewType() == AppConstant.MESSAGE_BALANCE_NOT_ENOUGH) {
                    tvMessageContent.setText(messageBean.getMsgContent());
                }
                setBoldNumber(messageBean.getMsgContent(), tvMessageContent);
                break;
            case AppConstant.MESSAGE_IM_MESSAGE:
                TextView tvMsgGet = view.findViewById(R.id.tv_msg_get);
                TextView tvMsgCheck = view.findViewById(R.id.tv_msg_check);
                TextView tvMsgContent = view.findViewById(R.id.tv_msg_content);
                tvMessageTime = view.findViewById(R.id.tv_message_time);
                BossImMessageBean bossImMessageBean = (BossImMessageBean) item;

                tvMessageTime.setText(TimeUtils.getFriendlyTimeSpanByNow(TimeUtils.millis2String(bossImMessageBean.getTime())));
                tvMsgContent.setText(bossImMessageBean.getMsgContent());
                tvMsgGet.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (mOnMsgManagerListener != null) {
                            mOnMsgManagerListener.onImMsgRead();
                        }
                    }
                });
                tvMsgCheck.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (mOnMsgManagerListener != null) {
                            mOnMsgManagerListener.onCheckMsg();
                        }
                    }
                });
                break;
            case AppConstant.MESSAGE_GUIDE_MESSAGE:
                GuidanceBean bean = (GuidanceBean) item;
                TextView tvStepCount = view.findViewById(R.id.tv_guide_step_count);
                TextView tvStepContent = view.findViewById(R.id.tv_step_content);
                TextView tvStepName = view.findViewById(R.id.tv_step_name);
                GuideStepBean guideStepBean = bean.getMerInfoStepList().get(bean.getCurrentProcess() - 1);
                tvStepCount.setText(bean.getCurrentProcess() + "/" + bean.getMerInfoStepList().size());
                tvStepName.setText(guideStepBean.getStepName() + ":" + guideStepBean.getStepTitle());
                tvStepContent.setText(guideStepBean.getStepDesc());
                break;
            case AppConstant.MESSAGE_ACTIVITY_APPLY:
                messageBean = (BossMessageBean) item;
                tvMessageTime = view.findViewById(R.id.tv_message_time);
                tvMessageTitle = view.findViewById(R.id.tv_message_title);
                tvMessageContent = view.findViewById(R.id.tv_message_content);
                ivIgnore = view.findViewById(R.id.iv_ignore);
                if (isHome) {
                    ivIgnore.setVisibility(View.VISIBLE);
                    ivIgnore.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if (mOnMsgManagerListener != null) {
                                mOnMsgManagerListener.onMsgClose(messageBean.getAppMsgCode());
                            }
                        }
                    });
                } else {
                    ivIgnore.setVisibility(View.GONE);
                }
                tvMessageTime.setText(TimeUtils.getFriendlyTimeSpanByNow(messageBean.getCreateTime()));
                tvMessageTitle.setText(messageBean.getMsgTitle());
                tvMessageContent.setText(messageBean.getMsgAttachedContent().getStoreTitle());
                break;
            case AppConstant.MESSAGE_MARKET_ADVICE:
                messageBean = (BossMessageBean) item;
                tvMessageTime = view.findViewById(R.id.tv_message_time);
                tvMessageTitle = view.findViewById(R.id.tv_message_title);
                ivIgnore = view.findViewById(R.id.iv_ignore);
                TextView tvTempName = view.findViewById(R.id.tv_temp_name);
                TextView tvTempTitle = view.findViewById(R.id.tv_temp_title);
                TextView tvAdviceName = view.findViewById(R.id.tv_advice_name);
                TextView tvAdviceShop = view.findViewById(R.id.tv_advice_shop);
                TextView tvAdviceDesc = view.findViewById(R.id.tv_advice_desc);
                ImageView ivIcon = view.findViewById(R.id.iv_temp_icon);
                if (isHome) {
                    ivIgnore.setVisibility(View.VISIBLE);
                    ivIgnore.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if (mOnMsgManagerListener != null) {
                                mOnMsgManagerListener.onMsgClose(messageBean.getAppMsgCode());
                            }
                        }
                    });
                } else {
                    ivIgnore.setVisibility(View.GONE);
                }
                tvMessageTime.setText(TimeUtils.getFriendlyTimeSpanByNow(messageBean.getCreateTime()));
                tvMessageTitle.setText(messageBean.getMsgTitle());

                tvTempName.setText(messageBean.getMsgAttachedContent().getTemplateName());
                tvTempTitle.setText(messageBean.getMsgAttachedContent().getTemplateDesc());
                tvAdviceName.setText(messageBean.getMsgAttachedContent().getTipTitle());
                tvAdviceShop.setText(messageBean.getMsgAttachedContent().getStoreName());
                tvAdviceDesc.setText(messageBean.getMsgAttachedContent().getTipDesc());
                ImageLoaderUtils.displayCircle(mContext, ivIcon, messageBean.getMsgAttachedContent().getTemplatePicUrl());
                break;
        }
    }

    private void setBoldNumber(String text, TextView textView) {
        if (text.contains("：")) {
            String before = text.split("：")[0];
            String after = text.split("：")[1];
            String number = BizUtil.getPureNumber(after);
            SpannableString sp = new SpannableString(text);
            int start = before.length() + 1;
            int end = before.length() + 1 + number.length();
            sp.setSpan(new RelativeSizeSpan(1.6f), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  //0.5f表示
            sp.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //粗体
            sp.setSpan(new ForegroundColorSpan(Color.parseColor("#343434")), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            textView.setText(sp);
        }
    }

    @Override
    public Paint dividerPaint(int position, RecyclerView parent) {
        Paint paint = new Paint();
        paint.setColor(Color.rgb(240, 240, 240));
        paint.setStrokeWidth(5);
        return paint;
    }

    public interface OnMsgManagerListener {
        void onMsgClose(String msgCode);

        void onImMsgRead();

        void onCheckMsg();

        void gotoActivity(String url, String title);
    }

    private OnMsgManagerListener mOnMsgManagerListener;

    public void setOnMsgCloseListener(OnMsgManagerListener listener) {
        mOnMsgManagerListener = listener;
    }

}
